Skip to content

Conversation

@fabiobaltieri
Copy link
Member

Add an optional suspend callback to let a downstream hid driver be notified of HID suspend and resume events.

Add an optional suspend callback to let a downstream hid driver be
notified of HID suspend and resume events.

Signed-off-by: Fabio Baltieri <fabiobaltieri@google.com>
@fabiobaltieri
Copy link
Member Author

@jfischer-no using this on an hid device to detect when the host goes into suspend, is this the right thing? Can't quite tell it from the controller apis apparently.

@zephyrbot zephyrbot added the area: USB Universal Serial Bus label Nov 8, 2025
@sonarqubecloud
Copy link

sonarqubecloud bot commented Nov 8, 2025

Copy link
Contributor

@tmon-nordic tmon-nordic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this approach better than relying on USB notifications in the application, because here the call is synchronous and therefore the state never gets out-of-sync.

@tmon-nordic tmon-nordic requested a review from josuah November 10, 2025 14:38
@fabiobaltieri
Copy link
Member Author

I like this approach better than relying on USB notifications in the application, because here the call is synchronous and therefore the state never gets out-of-sync.

Ok... what notifications are you referring to though?

@tmon-nordic
Copy link
Contributor

I like this approach better than relying on USB notifications in the application, because here the call is synchronous and therefore the state never gets out-of-sync.

Ok... what notifications are you referring to though?

usbd_msg_pub_simple(uds_ctx, USBD_MSG_SUSPEND, 0);
and
usbd_msg_pub_simple(uds_ctx, USBD_MSG_RESUME, 0);

@fabiobaltieri
Copy link
Member Author

@tmon-nordic ok but I think USBD_MSG_RESUME and USBD_MSG_SUSPEND are a different thing, that's the first thing I tried to use but it seems like they refer to the whole usb device controller, the ones in this PR and the ones I needed seemingly gets called when the host goes into suspend and stops polling the HID endpoint. I'm using them downstream to control a keyboard underglow and these two works fine. :-)

@tmon-nordic
Copy link
Contributor

@tmon-nordic ok but I think USBD_MSG_RESUME and USBD_MSG_SUSPEND are a different thing

No, they are the same. The difference is that USBD_MSG_RESUME and USBD_MSG_SUSPEND are subject to getting lost due to the IMHO bad design choice of

if (k_mem_slab_alloc(&usbd_msg_slab, (void **)&m_pkt, K_NO_WAIT)) {

when CONFIG_USBD_MSG_DEFERRED_MODE is not enabled.

@fabiobaltieri
Copy link
Member Author

fabiobaltieri commented Nov 10, 2025

No, they are the same. The difference is that USBD_MSG_RESUME and USBD_MSG_SUSPEND are subject to getting lost due to the IMHO bad design choice of

Ok very interesting, well, this is cheap enough and works reliably then... Thanks for the pointers.

@jfischer-no
Copy link
Contributor

No, they are the same. The difference is that USBD_MSG_RESUME and USBD_MSG_SUSPEND are subject to getting lost due to the IMHO bad design choice of

Ok very interesting, well, this is cheap enough and works reliably then... Thanks for the pointers.

This is nonsense again. The default mode is the way to provide notifications in a sequential way and not block the USB stack thread.

Comment on lines +106 to +111
/**
* This callback is called when the HID interface is suspended by the
* host. This callback is optional.
*/
void (*iface_suspended)(const struct device *dev, const bool suspended);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An interface cannot be suspended, and there is nothing like that in the HID specification. To get notifications when a USB device is suspended, please use USBD_MSG_SUSPEND and USBD_MSG_RESUME. There is no reason to have additional callbacks in each function/class implementation.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An interface cannot be suspended, and there is nothing like that in the HID specification. To get notifications when a USB device is suspended, please use USBD_MSG_SUSPEND and USBD_MSG_RESUME. There is no reason to have additional callbacks in each function/class implementation.

Ok, looked more into my application and yeah actually I managed to get it working with those two messages, the culprit may or may not have been a if (!usbd_can_detect_vbus(usbd_ctx)) {return;} in my code that was causing all messages to be ignored. :-)

Nevermind then thanks for the pointer, guess then these placeholder callback could go? Anyway closing this down.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: USB Universal Serial Bus

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants